﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using permit.App_Code.Entity;
using permit.App_Code.DataAccess;
using System.Text;
using System.Data;

namespace permit.opPermit
{
    public partial class InputPermit : System.Web.UI.Page
    {

        PermitAccess perAcc = new PermitAccess();
        DepartmentAccess depAcc = new DepartmentAccess();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                UserTable user = (UserTable)Session["UserTable"];
                if (user == null)
                {
                    Response.Write("<script>alert('请先登录！')</script>");
                    Response.Redirect("~/default.aspx");
                }
                AssginAccess assAcc = new AssginAccess();
                if (!assAcc.IsInputAssgin(user.USER_ID))
                {
                    Response.Redirect("~/main/Main.aspx?Sturt=Input");
                }

                if (user.DEP_PRINT == 1 || user.DEP_AREACODE != "620100")
                {
                    DepartmentTable dep = depAcc.GetDepartment(user.DEP_ID);

                    ListItem li = new ListItem(dep.DEP_NAME, dep.DEP_AREACODE);
                    ddlDep.Items.Add(li);
                }
                else
                {
                    DataSet ds = depAcc.GetDepartments();
                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                        ListItem li = new ListItem(dr["dep_Name"].ToString(), dr["dep_AreaCode"].ToString());
                        ddlDep.Items.Add(li);
                    }
                }
            }
        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            TextClear();
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            if (!MeetingManagerLib.DataTypeCheck.IsDigit(txtNum.Text))
            {
                Page.RegisterStartupScript("this5", "<script>alert('从业人数必须是数字！')</script>");
                return;
            }
            if (!MeetingManagerLib.DataTypeCheck.IsDigit(txtArea.Text))
            {
                Page.RegisterStartupScript("this6", "<script>alert('经营面积必须是数字！')</script>");
                return;
            }
            if (rblRep.SelectedIndex == -1)
            {
                Page.RegisterStartupScript("this6", "<script>alert('选择法人代表的身份性质！')</script>");
                return;
            }

            PermitTable permit = new PermitTable();

            permit.Per_Department = txtDepartment.Text;
            permit.Per_Address = txtAddress.Text;
            permit.Per_Type = ddlType.SelectedValue;
            permit.Per_Representative = txtRep.Text + '（'+ rblRep.SelectedValue + '）';
            permit.Per_Tel = txtTel.Text;
            if (txtArea.Text == "")
            {
                permit.Per_Area = 0.0;
            }
            else
            {
                permit.Per_Area = double.Parse(txtArea.Text);
            }
            if (txtNum.Text == "")
            {
                permit.Per_Number = 0;
            }
            else
            {
                permit.Per_Number = Convert.ToInt32(txtNum.Text);
            }
            
            permit.Per_AreaCode = ddlDep.SelectedValue;

            StringBuilder sb = new StringBuilder();
            if (RadioButtonList1.SelectedIndex >= 0)
            {
                sb.Append(RadioButtonList1.SelectedValue);
            }
            if (RadioButtonList2.SelectedIndex >= 0)
            {
                if (sb.Length > 0)
                {
                    sb.Append("，" + RadioButtonList2.SelectedValue);
                }
                else
                {
                    sb.Append(RadioButtonList2.SelectedValue);
                }
            }
            if (RadioButtonList3.SelectedIndex >= 0)
            {
                if (sb.Length > 0)
                {
                    sb.Append("，" + RadioButtonList3.SelectedValue);
                }
                else
                {
                    sb.Append(RadioButtonList3.SelectedValue);
                }
            }
            if (RadioButtonList4.SelectedIndex >= 0)
            {
                if (sb.Length > 0)
                {
                    sb.Append("，" + RadioButtonList4.SelectedValue);
                }
                else
                {
                    sb.Append(RadioButtonList4.SelectedValue);
                }
            }
            if (RadioButtonList5.SelectedIndex >= 0)
            {
                if (sb.Length > 0)
                {
                    sb.Append("，" + RadioButtonList5.SelectedValue);
                }
                else
                {
                    sb.Append(RadioButtonList5.SelectedValue);
                }
            }

            sb.Append("*");

            permit.Per_Mark = sb.ToString();
            permit.Per_Scope = txtScope.Text;

            permit.Per_Flag = 0;
            permit.Per_Count = 0;
            permit.Per_InputDate = DateTime.Now;

            UserTable user = (UserTable)Session["UserTable"];
            permit.Per_InputPersion = user.NAME;

            int count = perAcc.InsertPermit(permit);

            if (count > 0)
            {
                Page.RegisterStartupScript("this7", "<script>alert('已添加成功！')</script>");
                TextClear();
            }
            else
            {
                Page.RegisterStartupScript("this8", "<script>alert('添加失败，请重试！')</script>");
            }

        }

        public void TextClear()
        {
            txtDepartment.Text = "";
            txtRep.Text = "";
            txtAddress.Text = "";
            ddlType.SelectedIndex = 0;
            txtArea.Text = "";
            txtNum.Text = "";
            txtTel.Text = "";
            txtScope.Text = "";
            ddlDep.SelectedIndex = 0;
            rblRep.SelectedIndex = -1;

            RadioButtonList1.SelectedIndex = -1;
            RadioButtonList2.SelectedIndex = -1;
            RadioButtonList3.SelectedIndex = -1;
            RadioButtonList4.SelectedIndex = -1;
            RadioButtonList5.SelectedIndex = -1;
        }

        protected void btnMark_Click(object sender, EventArgs e)
        {
            RadioButtonList1.SelectedIndex = -1;
            RadioButtonList2.SelectedIndex = -1;
            RadioButtonList3.SelectedIndex = -1;
            RadioButtonList4.SelectedIndex = -1;
            RadioButtonList5.SelectedIndex = -1;
        }


    }
}
